package utils;

import com.sun.xml.internal.ws.server.ServerRtException;
import controller.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SelectUser extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        System.out.println(phone);
        System.out.println(password);
        //String sss= "select u_id from user_info where u_phone=phone and u_password =password";
        //String str = "select u_id,u_name from user_info where u_phone='"+phone+"'and u_password="+password;
        String str = "SELECT * from user_info where u_phone = "+phone+" and u_password = "+"'"+password+"'";
        //System.out.println(str);
        if(phone!=""&&password!=""){
        try {
            Connection connection = DBUtils.Connection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(str);
            System.out.println(resultSet.next());
            //resp.sendRedirect(req.getContextPath()+"/html/loading.html");
            //resp.sendRedirect(req.getContextPath()+"html/success.html");
            String re = "";
            while (resultSet.next()){
                re += resultSet.getString("u_name");

            }

            //            int i;
//            if(!next){
//                i=1;
//            }else {
//                i=2;
//            }
//
//
//            if (i==2){
//                resp.sendRedirect(req.getContextPath()+"html/loading.html");
//            }
//            if (!next){
//                resp.sendRedirect(req.getContextPath()+"html/denglushibai.html");
//            }

            //有问题
            System.out.println(re);
                    if (re!=""){

    //
                    resp.sendRedirect(req.getContextPath()+"html/denglushibai.html");
                    System.out.println("aaaaa");

                }
                else{


    //                req.setAttribute("phone",phone);
                    resp.sendRedirect(req.getContextPath()+"html/loading.html");
                    System.out.println("bbbbbbbb");

                }

        } catch (Exception e) {
            e.printStackTrace();
        }
        }else{
            resp.sendRedirect(req.getContextPath()+"html/denglushibai.html");

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
